//
//  YFMineViewModel.m
//  project
//
//  Created by yefei on 2019/5/6.
//  Copyright © 2019 健健. All rights reserved.
//

#import "YFMineViewModel.h"
#import "YFDongtaiModel.h"

@interface YFMineViewModel ()

@property (nonatomic,strong) NSArray <YFDongtaiRecordModel *> *dongtais;

@end

@implementation YFMineViewModel

- (void)updateData {
    
    NSMutableArray<YFMineCellViewModel *> *VMs = [NSMutableArray arrayWithCapacity:self.dongtais.count];
    for (YFDongtaiRecordModel *model in self.dongtais) {
        YFMineCellViewModel *vm = [[YFMineCellViewModel alloc] initWithModel:model];
        [VMs addObject:vm];
    }
    self.cellVMs = VMs;
}

- (YFMineViewModel *(^)(NSInteger page, BOOL isRefresh, void(^compelteion)(BOOL isSuccess, BOOL isBottom, NSString *error)))loadMyDongtai {
    @weakify(self)
    return ^YFMineViewModel *(NSInteger page, BOOL isRefresh, void(^compelteion)(BOOL isSuccess, BOOL isBottom, NSString *error)) {
        @strongify(self)
        NSString *url = [Api stringByAppendingString:MyDongtaiApi];
        NSDictionary *param = @{
                                @"userId": [HJUserManager getUserModel].userId,
                                @"pageCurrent": @(page),
                                @"pageSize":@20,
                                };
        @weakify(self)
        [HJHttpTool post:url params:param success:^(HJResponeds *result) {
            @strongify(self)
            if (page==1) {
                self.dongtais = @[];
            }
            YFDongtaiModel *model = [YFDongtaiModel yy_modelWithDictionary:result.data];
            self.dongtais = self.dongtais.addArray(model.records, isRefresh);
            [self updateData];
            BOOL isBottom = page == model.pages;
            compelteion(YES, isBottom, nil);
        } failure:^(id result) {
            compelteion(NO, NO, result);
        }];
        return self;
    };
}

- (YFMineViewModel *(^)(NSString *userId, NSArray<NSString *> *ablumIds, void(^compelteion)(BOOL isSuccess, NSString *error)))deleteDongtai {
    return ^YFMineViewModel *(NSString *userId, NSArray<NSString *> *ablumIds, void(^compelteion)(BOOL isSuccess, NSString *error)) {
        NSString *url = [Api stringByAppendingString:DeleteDongtaiApi];
        // LKLog(jsonString);
        NSDictionary *param = @{
                                @"userId":userId,
                                @"ablumIds": ablumIds.jsonArray,
                                };
        [HJHttpTool post:url params:param success:^(HJResponeds *result) {
            LKLog(@"%@", result);
            compelteion(YES, nil);
        } failure:^(id result) {
            LKLog(@"%@", result);
            compelteion(NO, result);
        }];
        return  self;
    };
}

- (NSArray<YFDongtaiRecordModel *> *)dongtais {
    if (!_dongtais) {
        _dongtais = NSArray.new;
    }
    return _dongtais;
}

- (NSArray<YFMineCellViewModel *> *)cellVMs {
    if (!_cellVMs) {
        _cellVMs = NSArray.new;
    }
    return _cellVMs;
}

@end
